/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminPanel.java
 *
 * Created on Apr 29, 2011, 5:02:28 PM
 */

package courseScheduler;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author david
 */
public class AdminPanel extends SpecialContentPanel {

    /** Creates new form AdminPanel */
    public AdminPanel(MainThread newParent) {
        parent = newParent;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        FirstNameInput = new javax.swing.JTextField();
        MiddleInitialInput = new javax.swing.JTextField();
        FNameLabel = new javax.swing.JLabel();
        MILabel = new javax.swing.JLabel();
        LNameLabel = new javax.swing.JLabel();
        LastNameInput = new javax.swing.JTextField();
        typeChooser = new javax.swing.JComboBox();
        AddressLabel = new javax.swing.JLabel();
        Address1Input = new javax.swing.JTextField();
        Address2Input = new javax.swing.JTextField();
        CityInput = new javax.swing.JTextField();
        CityLabel = new javax.swing.JLabel();
        StateCombo = new javax.swing.JComboBox();
        ZipcodeLabel = new javax.swing.JLabel();
        ZipcodeInput = new javax.swing.JTextField();
        AddButton = new javax.swing.JButton();
        SearchButton = new javax.swing.JButton();
        DeleteButton = new javax.swing.JButton();
        PhoneLabel = new javax.swing.JLabel();
        PhoneInput = new javax.swing.JTextField();
        EmailLabel = new javax.swing.JLabel();
        EmailInput = new javax.swing.JTextField();
        IDLabel = new javax.swing.JLabel();
        IDField = new javax.swing.JTextField();
        UpdateButton = new javax.swing.JButton();

        MiddleInitialInput.setColumns(1);

        FNameLabel.setText("Fist Name");

        MILabel.setText("Middle Inital");

        LNameLabel.setText("Last Name");

        typeChooser.setModel(new DefaultComboBoxModel(typeComboList));

        AddressLabel.setText("Address");

        CityLabel.setText("City");

        StateCombo.setModel(new DefaultComboBoxModel(stateComboList));

        ZipcodeLabel.setText("Zipcode");

        AddButton.setText("Add");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        SearchButton.setText("Search");
        SearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchButtonActionPerformed(evt);
            }
        });

        DeleteButton.setText("Delete");
        DeleteButton.setEnabled(false);

        PhoneLabel.setText("Phone");

        EmailLabel.setText("Email");

        IDLabel.setText("ID");

        IDField.setEditable(false);
        IDField.setEnabled(false);
        IDField.setFocusable(false);

        UpdateButton.setText("Update");
        UpdateButton.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(FNameLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                                .addComponent(FirstNameInput, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(LNameLabel)
                                .addGap(18, 18, 18)
                                .addComponent(LastNameInput, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(MILabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(MiddleInitialInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(typeChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(PhoneLabel)
                                    .addComponent(EmailLabel))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(EmailInput)
                                    .addComponent(PhoneInput, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE))))
                        .addGap(35, 35, 35))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(IDLabel)
                            .addComponent(CityLabel)
                            .addComponent(AddressLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Address2Input, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                            .addComponent(Address1Input, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(IDField)
                                    .addComponent(CityInput, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(StateCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(ZipcodeLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ZipcodeInput, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(DeleteButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(UpdateButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                                        .addComponent(SearchButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addContainerGap(20, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(FNameLabel)
                    .addComponent(MILabel)
                    .addComponent(FirstNameInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(MiddleInitialInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(typeChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LNameLabel)
                    .addComponent(LastNameInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(EmailLabel)
                    .addComponent(EmailInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PhoneLabel)
                    .addComponent(PhoneInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AddressLabel)
                    .addComponent(Address1Input, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Address2Input, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CityInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CityLabel)
                    .addComponent(StateCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ZipcodeLabel)
                    .addComponent(ZipcodeInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AddButton)
                    .addComponent(IDLabel)
                    .addComponent(IDField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DeleteButton)
                    .addComponent(SearchButton)
                    .addComponent(UpdateButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        String [] temp = makeTemp();
        String user = temp[0] + " " + temp[2];
        fillValuesAndArray(temp);
        createQuery(user);
        System.out.println(sql);
        try {
            parent.getConn().createStatement().executeUpdate(sql);
        } catch (SQLException ex) {
            Logger.getLogger(AdminPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_AddButtonActionPerformed

    private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchButtonActionPerformed
        String [] temp = makeTemp();
        fillValuesAndArray(temp);
        new SearchFrame(Array,Values,parent.getConn()).setVisible(true);
    }//GEN-LAST:event_SearchButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JTextField Address1Input;
    private javax.swing.JTextField Address2Input;
    private javax.swing.JLabel AddressLabel;
    private javax.swing.JTextField CityInput;
    private javax.swing.JLabel CityLabel;
    private javax.swing.JButton DeleteButton;
    private javax.swing.JTextField EmailInput;
    private javax.swing.JLabel EmailLabel;
    private javax.swing.JLabel FNameLabel;
    private javax.swing.JTextField FirstNameInput;
    private javax.swing.JTextField IDField;
    private javax.swing.JLabel IDLabel;
    private javax.swing.JLabel LNameLabel;
    private javax.swing.JTextField LastNameInput;
    private javax.swing.JLabel MILabel;
    private javax.swing.JTextField MiddleInitialInput;
    private javax.swing.JTextField PhoneInput;
    private javax.swing.JLabel PhoneLabel;
    private javax.swing.JButton SearchButton;
    private javax.swing.JComboBox StateCombo;
    private javax.swing.JButton UpdateButton;
    private javax.swing.JTextField ZipcodeInput;
    private javax.swing.JLabel ZipcodeLabel;
    private javax.swing.JComboBox typeChooser;
    // End of variables declaration//GEN-END:variables


    java.awt.Component fields [] = {FirstNameInput,MiddleInitialInput,LastNameInput,Address1Input,Address2Input,CityInput,StateCombo,ZipcodeInput,PhoneInput,EmailInput};
    String [] attributes = {"f_name","m_initial","l_name","address_one","address_two","city","state_","zipcode","phone","e_mail","type"};
    String [] Values,Array;
    private String [] typeComboList = {" ","Student","Faculty"};
    private String [] stateComboList = {" ", "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NY", "NC", "ND", "OH", "OK", "OR", "PW", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WK", "WI", "WY" };
    private String sql;

    public String[] makeTemp(){
        String [] temp = new String[11];
        temp[0] = FirstNameInput.getText().toLowerCase();try{
                temp[1] = MiddleInitialInput.getText().charAt(0) + "\0";
            }
            catch (java.lang.StringIndexOutOfBoundsException ex){
                temp[1] = "";
            }
        temp[2] = LastNameInput.getText().toLowerCase();
            
        temp[3] = Address1Input.getText().toLowerCase();
        temp[4] = Address2Input.getText().toLowerCase();
        temp[5] = CityInput.getText().toLowerCase();
        temp[6] = stateComboList[StateCombo.getSelectedIndex()].toLowerCase();
        temp[7] = ZipcodeInput.getText().toLowerCase();
        temp[8] = PhoneInput.getText().toLowerCase();
        temp[9] = EmailInput.getText().toLowerCase();
        temp[10] = typeComboList[typeChooser.getSelectedIndex()].toLowerCase();
            if (temp[10].equals(" ")){
                temp[10] = "";
            }
        return temp;
    }

    private void fillValuesAndArray(String [] temp){
        String [] tempArray;
        String [] tempValues;
        Values = new String[0];
        Array = new String[0];
        for(int i = 0; i < 11; i ++){
            if (i == 6 && temp[6].equals(" ")){
                temp[6] = "";
            }
            if (!temp[i].isEmpty()){
                try{
                    tempValues = Values;
                    System.out.println("assign temp");
                    Values = new String[tempValues.length + 1];
                    System.out.println("new Array");
                    System.arraycopy(tempValues, 0, Values, 0, tempValues.length);
                    System.out.println("array copy");
                    Values[tempValues.length] = temp[i];
                    System.out.println("copylast value");
                    tempArray = Array;
                    Array = new String[tempArray.length + 1];
                    System.arraycopy(tempArray, 0, Array, 0, tempArray.length);
                    Array[tempArray.length] = attributes[i];
                }
                catch(Exception ex){
                    Values = new String[1];
                    Values[0] = temp[i];
                    Array = new String[1];
                    Array [0] = attributes[i];
                    System.out.println(temp[i] + " " + Values[0] + " " + Array[0]);
                }
            }
        }
    }

    private void createQuery(String user){
        sql = "INSERT INTO person (" + Array[0] + "";
        for (int i = 1; i < Array.length; i ++){
            sql += "," + Array[i];
        }
        sql += ",user_name,password) VALUES (";
        if (Array[0].equals("m_initial")){
            sql += "\'" + Values [0].charAt(0) + "\'";
        }
        else {
            sql += "\"" + Values [0] + "\"";
        }
        for (int i = 1; i < Array.length; i ++){
            if (Array[i].equals("m_initial")){
                sql += ",\'" + Values[i].charAt(0) + "\'";
            }
            else{
                sql += ",\"" + Values[i] + "\"";
            }
        }
        sql += ",\"" + user + "\",password(\"password\"));";
    }
}
